﻿@using KPIS.GERP.Models.ICM
@model IEnumerable<Committee>

@{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    ViewBag.Title = "คณะกรรมการ";
    ViewBag.Descr = "ข้อมูล" + ViewBag.Title + " ที่ร้องขอ";

    var service = new KPIS.GERP.WEB.GIMS.DisposeService.DisposeServiceClient();
    int DisposeId = ViewBag.DisposeId;
    
    var modelDispose = service.SelectDispose(new Dispose { DisposeId = DisposeId, RecordStatus = "!" + KPIS.GERP.Models.SYS.RecordStatus.DeletedStatus }
    , new[] { "MasResourceType", "JobSite", "Warehouse", "DisposeType", "Employee" }
    ).FirstOrDefault();

    // ถ้ายังไม่มีการอนุมัติหรือส่งอนุมัติก็จะสามารถ เพิ่ม, ลบ, แก้ไข คณะกรรมการได้
    var Expresstion = modelDispose.DisposeStatus != KPIS.GERP.Models.SYS.RecordStatus.ApprovedStatus && modelDispose.DisposeStatus != KPIS.GERP.Models.SYS.RecordStatus.ProcessStatus;
}

@using (Html.BeginForm("DeleteCommitteeAll", "Dispose", FormMethod.Post, new { id = "form" }))
{
     @Html.AntiForgeryToken()

    <div class="row-fluid">

        @if ( Expresstion )
        {
            <div class="block-button-menu-above">
                @Html.ActionLink("เพิ่ม", "CreateCommittee", "Dispose", new { id = DisposeId }
                                 , new { @class = "btn btn-small btn-success button-standard-width", role = "button" })
                @Html.ButtonDeleteAll()
            </div>
        }

        <div class="alert-error">@Html.ValidationSummary()</div>
        @* Header Table *@
        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
                    @ViewBag.Title
                </div>
                @Html.Partial("_ExportButton")
            </div>
        </div>

        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    <th  style="display:none"></th>
                    <th class="center" style="width: 50px;">
                        <label> <input type="checkbox" /> <span class="lbl"></span> </label>
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.ComFunctionId)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Code)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.ComPositionId)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.EmpName)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.EmpPositionName)
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @{
                    var data = Model ?? new List<Committee>();
                    // oRDER BY ชุดที่ -> หน้าที่ของกรรมการ -> ตำแหน่งของกรรมการ
                    var resorted = data.OrderBy(o => o.Code).ThenBy(p => p.ComFunctionId).ThenBy(q => q.ComPositionId).ToList();
                }

                @foreach (var m in resorted)
                {
                    <tr>
                        <td style="display:none"></td>
                        <td class="center">
                            <!-- Check box all for delete multiple records -->
                            <label>
                                <input type="checkbox" name="deleteIds" value="@(m.CommitteeId)" />
                                <span class="lbl"></span>
                            </label>
                        </td>
                        <td>
                            @if (ViewBag.EmptyLayout != true)
                            {
                                @Html.ActionLink(m.CommitteeFunction.LookUpName, "CommitteeDetails", new { id = m.CommitteeId })
                            }
                            else
                            {
                                @Html.DisplayFor(model => m.CommitteeFunction.LookUpName)
                            }
                        </td>
                        <td>
                            @if (ViewBag.EmptyLayout != true)
                            {
                                @Html.ActionLink(m.Code, "CommitteeDetails", new { id = m.CommitteeId })
                            }
                            else
                            {
                                @Html.DisplayFor(model => m.Code)
                            }
                        </td>
                        <td>
                            @if (ViewBag.EmptyLayout != true)
                            {
                                @Html.ActionLink(m.CommitteePosition.LookUpName, "CommitteeDetails", new { id = m.CommitteeId })
                            }
                            else
                            {
                                @Html.DisplayFor(model => m.CommitteePosition.LookUpName)
                            }
                        </td>
                        <td>
                            @if (ViewBag.EmptyLayout != true)
                            {
                                @Html.ActionLink(m.EmpName, "CommitteeDetails", new { id = m.CommitteeId })
                            }
                            else
                            {
                                @m.EmpName
                            }
                        </td>
                        <td>
                            @Html.DisplayFor(model => m.Employee.Position.Name)
                        </td>
                        <td>
                            @if (ViewBag.EmptyLayout != true && Expresstion)
                            {
                                @Html.Partial("_ButtonMenuEachRow", new KPIS.GERP.Models.ViewModel.ExportGrid
                                    {
                                        Id = m.CommitteeId,
                                        DetailsUrl = Url.Action("CommitteeDetails") + "/" + m.CommitteeId,
                                        EditUrl = Url.Action("EditCommittee") + "/" + m.CommitteeId,
                                        DeleteUrl = Url.Action("DeleteCommittee") + "/" + m.CommitteeId + "?DisposeId=" + DisposeId
                                    })
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}



@section scripts {
    
	<script type="text/javascript">
		$(function () {
		    var oTable1 = $('#table').dataTable({
		        "aoColumns": [
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    { "bSortable": false }
		        ]
		    });

		    $('table th input:checkbox').on('click', function () {
		        var that = this;
		        $(this).closest('table').find('tr > td:first-child input:checkbox')
				.each(function () {
					this.checked = that.checked;
					$(this).closest('tr').toggleClass('selected');
				});
		    });

		    $('[data-rel=tooltip]').tooltip();

		    $("#form").submit(function () {
		        var currentForm = this;
		        bootbox.confirm(function (result) {
		            var _result = result;
		            if (result) {
		                $.ajax({
		                    url: '@(Url.Action("DeleteCommitteeAll", "Dispose"))',
		                    type: "post",
		                    dataType: "json",
		                    data: $(currentForm).serialize(),
		                    success: function (result) {
		                        if (result.success) {
		                            location.reload();
		                        } else {
		                        }
		                    }
		                });
		            }
		        })
		        return false;

		    });

		})

	</script>

}
